package com.tianji.learning.mq;


import com.tianji.api.dto.remark.LikedTimesDTO;
import com.tianji.api.dto.trade.OrderBasicDTO;
import com.tianji.common.constants.MqConstants;
import com.tianji.common.utils.CollUtils;
import com.tianji.learning.domain.po.InteractionReply;
import com.tianji.learning.mapper.InteractionReplyMapper;
import com.tianji.learning.service.IInteractionReplyService;
import com.tianji.learning.service.ILearningLessonService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Component
@Slf4j
@RequiredArgsConstructor
public class LikeTimesChangeListener {
    private final IInteractionReplyService replyService;
    private final InteractionReplyMapper replyMapper;

    //交换机，路由器，key
    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(name = "qa.liked.times.queue1", durable = "true"),
                    exchange = @Exchange(value = MqConstants.Exchange.LIKE_RECORD_EXCHANGE, type = ExchangeTypes.TOPIC),
                    key = MqConstants.Key.QA_LIKED_TIMES_KEY
            )
    )
    public void listenLikeTimesChange(List<LikedTimesDTO> dtoList) {
        //1,判断
        if (CollUtils.isEmpty(dtoList)) {
            return;
        }
        List<InteractionReply> replyList = new ArrayList<>();
        for (LikedTimesDTO dto : dtoList) {
            //将原有的点赞数加上dtolist里面的点赞数（id要一样）
            InteractionReply reply1 = new InteractionReply();
            reply1.setId(dto.getBizId());
            reply1.setLikedTimes(dto.getLikedTimes());
            replyList.add(reply1);
        }
        //3.批量更新
        replyService.updateBatchById(replyList);
    }
}
